/**

 @Name：layuiAdmin 系统管理->课程管理
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL

 */


layui.define(['table', 'form'], function(exports){
    var $ = layui.$
        ,admin = layui.admin
        ,view = layui.view
        ,table = layui.table
        ,form = layui.form;

    //课程管理表格渲染
    table.render({
        elem: '#LAY-course-manage'
        ,url: '/getAllCoursesPageServlet' //课程分页查询接口
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{hide: 'courseId', width: 100, title: 'ID', sort: true}
            ,{field: 'courseName', title: '课程名称', minWidth: 200}
            ,{field: 'courseNum', title: '课程编号', width: 180}
            ,{field: 'courseScore', title: '学分', width: 80}
            ,{field: 'courseTimes', title: '总学时', width: 100}
            ,{field: 'courseLtimes', title: '讲授学时', width: 120}
            ,{field: 'courseStimes', title: '实践学时', width: 120}
            ,{field: 'courseFlag', title: '状态', width: 80, templet: function(d){
                    return d.courseFlag === '0' ? '启用' : '禁用';
                }}
            ,{title: '操作', width: 180, align:'center', fixed: 'right', toolbar: '#table-system-course'}
        ]]
        ,page: true
        ,limit: 10
        ,height: 'full-320'
        ,text: '对不起，课程数据加载异常！'
    });

    //监听课程表格工具条
    table.on('tool(LAY-course-manage)', function(obj){
        var data = obj.data;

        if(obj.event === 'del'){ //删除课程
            layer.confirm('确定要删除该课程吗？删除后无法恢复！', function(index){
                $.ajax({
                    type:'POST'
                    ,url:'/deleteCourseServlet'
                    ,data:{"cid":data.courseId}
                    ,success:function(result){
                        if(result === "Ok"){
                            layer.alert('课程删除成功', {icon: 6});
                            obj.del(); //删除表格行
                        } else {
                            layer.alert(result, {title: '删除失败'});
                        }
                    }
                });
                layui.table.reload('LAY-course-manage'); //重载表格
                layer.close(index);
            });
        } else if(obj.event === 'edit'){ //编辑课程
            admin.popup({
                title: '编辑课程信息'
                ,area: ['550px', '500px']
                ,id: 'LAY-popup-course-edit'
                ,success: function(layero, index){
                    view(this.id).render('course/update', data).done(function(){
                        form.render(null, 'layuiadmin-form-course');

                        // 监听编辑提交
                        form.on('submit(LAY-course-modify)', function(data){
                            var field = data.field;

                            $.ajax({
                                type: 'POST',
                                url: '/updateCourseServlet',
                                data: field,
                                success: function(result) {
                                    if (result === "Ok") {
                                        layer.alert("课程更新成功", {icon: 6});
                                        layui.table.reload('LAY-course-manage');
                                        layer.close(index);
                                    } else {
                                        layer.alert(result, {title: '更新失败'});
                                    }
                                }
                            });
                            return false;
                        });
                    });
                }
            });
        }
    });

    //渲染表单元素
    form.render(null, 'layadmin-course-formlist');

    //监听搜索
    form.on('submit(LAY-course-front-search)', function(data){
        var field = data.field;

        //执行表格重载
        table.reload('LAY-course-manage', {
            page: {
                curr: 1 //重置为第一页
            }
            ,where: field
            ,method: 'post'
        });
    });

    //定义操作事件
    var active = {
        batchdel: function(){ //批量删除
            var checkStatus = table.checkStatus('LAY-course-manage')
                ,checkData = checkStatus.data; //获取选中的数据

            if(checkData.length === 0){
                return layer.msg('请先选择要删除的课程');
            }

            layer.prompt({
                formType: 1
                ,title: '敏感操作，请输入验证口令'
            }, function(value, index){
                layer.close(index);

                layer.confirm('确定删除选中的课程吗？此操作不可恢复！', function(index) {
                    //模拟批量删除逻辑
                    var courseIds = checkData.map(item => item.courseId).join(",");

                    $.ajax({
                        type:'POST'
                        ,url:'/batchDeleteCourseServlet' //假设存在批量删除接口
                        ,data:{"courseIds":courseIds}
                        ,success:function(result){
                            if(result === "Ok"){
                                layer.alert('批量删除成功', {icon: 6});
                                table.reload('LAY-course-manage');
                            } else {
                                layer.alert(result, {title: '删除失败'});
                            }
                        }
                    });
                });
            });
        }
        ,add: function(){ //添加课程
            admin.popup({
                title: '添加新课程'
                ,area: ['550px', '500px']
                ,id: 'LAY-popup-course-add'
                ,success: function(layero, index){
                    view(this.id).render('course/add').done(function(){
                        form.render(null, 'layuiadmin-form-course');

                        // 监听添加提交
                        form.on('submit(LAY-course-add)', function(data){
                            var field = data.field;

                            $.ajax({
                                type: 'POST',
                                url: '/addCourseServlet',
                                data: field,
                                success: function(result) {
                                    if (result === "Ok") {
                                        layer.alert("课程添加成功", {icon: 6});
                                        layui.table.reload('LAY-course-manage');
                                        layer.close(index);
                                    } else {
                                        layer.alert(result, {title: '添加失败'});
                                    }
                                }
                            });
                            return false;
                        });
                    });
                }
            });
        }
    };

    //绑定按钮事件
    $('.layui-btn.layuiadmin-btn-courseadmin').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    exports('course', {})
});